package blog.controller;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import blog.entity.Link;
import blog.enums.PageStatus;
import blog.service.LinkService;

@Controller
@RequestMapping("/link")
public class LinkController {
	@Resource
	LinkService linkService;
	
	@RequestMapping("")
	public String index(ModelMap m) {
		List<Link> linkList = linkService.findAll();
		m.put("linkList", linkList);
		return "Link/link-list";
	}
	
	/**
	 * 删除链接
	 */
	@RequestMapping("/delete/{linkId}")
	public String delete(@PathVariable("linkId") Integer linkId)  {
		linkService.deleteLinkById(linkId);;
		return "forward:/link";
	}
	
	/**
	 * 进入链接添加页面
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String gotoAddPage(ModelMap m) {
		List<Link> linkList = linkService.findAll();
		m.put("linkList", linkList);
		return "Link/link-add";
	}
	/**
	 * 进入链接添加跳转
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Link link) {
		if(link.getLinkOrder()==null) {
			link.setLinkOrder(0);
		}
		link.setLinkStatus(PageStatus.NORMAL.getValue());
		link.setLinkCreateTime(LocalDateTime.now());
		linkService.add(link);
		return "forward:/link";
	}
	
}
